package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.Doctor;
import service.DoctorService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @GetMapping("/findAll")
    @ResponseBody
    public List<Doctor> findAll() {
        return doctorService.findAll();
    }

    @GetMapping("/findById")
    @ResponseBody
    public Doctor findById(@RequestParam String docId) {
        return doctorService.findById(docId);
    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody Doctor doctor) {
        Map<String, Object> result = new HashMap<>();
        int count = doctorService.add(doctor);
        result.put("success", count > 0);
        result.put("message", count > 0 ? "添加成功" : "添加失败");
        return result;
    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Doctor doctor) {
        Map<String, Object> result = new HashMap<>();
        int count = doctorService.update(doctor);
        result.put("success", count > 0);
        result.put("message", count > 0 ? "更新成功" : "更新失败");
        return result;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam String docId) {
        Map<String, Object> result = new HashMap<>();
        int count = doctorService.delete(docId);
        result.put("success", count > 0);
        result.put("message", count > 0 ? "删除成功" : "删除失败");
        return result;
    }

    @GetMapping("/findByDepartId1")
    @ResponseBody
    public List<Doctor> findByDepartId1(@RequestParam String depart_id1) {
        return doctorService.findByDepartId1(depart_id1);
    }
    @GetMapping("/findByUserName")
    @ResponseBody
    public List<Doctor> findByUserName(@RequestParam String user_name) {
        return doctorService.findByUserName(user_name);
    }
    @GetMapping("/findByDocName")
    @ResponseBody
    public List<Doctor> findByDocName(@RequestParam String doc_name) {
        return doctorService.findByDocName(doc_name);
    }
}